package cn.wetcland.task;

import cn.TCSP;
import cn.hutool.core.io.FileUtil;
import cn.util.TCUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.Calendar;
import java.util.Collection;

public class WorldResetTask {

    public void runTask() {
        Bukkit.getScheduler().runTaskTimerAsynchronously(TCSP.getTcsp(), () -> {
            Calendar instance = Calendar.getInstance();
            int day = instance.get(Calendar.DAY_OF_MONTH);
            int hour = instance.get(Calendar.HOUR_OF_DAY);
            int minute = instance.get(Calendar.MINUTE);
            if (day == 1 && hour == 0 && minute == 1) {
                Bukkit.broadcastMessage("§b========重要通知========");
                Bukkit.broadcastMessage("§c服务器将在30秒后开始重置 资源/末地/地狱 世界, 请做好准备");
                Bukkit.broadcastMessage("§c届时服务器将开启白名单");
                Bukkit.broadcastMessage("§c服务器将在30秒后开始重置 资源/末地/地狱 世界, 请做好准备");
                Bukkit.broadcastMessage("§c届时服务器将开启白名单");
                Bukkit.broadcastMessage("§c服务器将在30秒后开始重置 资源/末地/地狱 世界, 请做好准备");
                Bukkit.broadcastMessage("§c届时服务器将开启白名单");
                Bukkit.getScheduler().runTaskLater(TCSP.getTcsp(), () -> {
                    Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();
                    for (Player player : onlinePlayers) {
                        player.kickPlayer("§a服务器正在重置资源/末地/地狱, 将暂时开启白名单,请稍后再尝试登陆~");
                    }
                    Bukkit.setWhitelist(true);
                    ConsoleCommandSender consoleSender = Bukkit.getConsoleSender();
                    // 删除世界
                    Bukkit.dispatchCommand(consoleSender, "mw delete ResWorld");
                    Bukkit.dispatchCommand(consoleSender, "mw delete world_nether");
                    Bukkit.dispatchCommand(consoleSender, "mw delete world_the_end");
                    File file = new File("ResWorld");
                    File file1 = new File("world_nether");
                    File file2 = new File("world_the_end");
                    FileUtil.del(file);
                    FileUtil.del(file1);
                    FileUtil.del(file2);
                    // 生成世界
                    Bukkit.dispatchCommand(consoleSender, "mw create ResWorld Normal");
                    Bukkit.dispatchCommand(consoleSender, "mw create world_nether Nether");
                    Bukkit.dispatchCommand(consoleSender, "mw create world_the_end The_End");
                    System.out.println("§a成功生成世界");
                    // 加载世界
                    Bukkit.dispatchCommand(consoleSender, "mw load ResWorld");
                    Bukkit.dispatchCommand(consoleSender, "mw load world_nether");
                    Bukkit.dispatchCommand(consoleSender, "mw load world_the_end");
                    System.out.println("§a成功加载世界");
                    System.out.println("§b服务器重置世界完成，正在关闭白名单");
                    Bukkit.setWhitelist(false);
                    // 重置世界出生点
                    World resWorld = Bukkit.getWorld("ResWorld");
                    World world_nether = Bukkit.getWorld("world_nether");
                    World world_the_end = Bukkit.getWorld("world_the_end");
                    Location spawnLocation = resWorld.getSpawnLocation();
                    Location spawnLocation1 = world_nether.getSpawnLocation();
                    Location spawnLocation2 = world_the_end.getSpawnLocation();
                    if (spawnLocation.getY() >= 225) spawnLocation.setY(spawnLocation.getY() - 10);
                    if (spawnLocation1.getY() >= 225) spawnLocation1.setY(spawnLocation1.getY() - 10);
                    if (spawnLocation2.getY() >= 225) spawnLocation2.setY(spawnLocation2.getY() - 10);
                    Block blockAt = resWorld.getBlockAt(spawnLocation);
                    blockAt.setType(Material.BEDROCK);
                    spawnLocation.setY(spawnLocation.getY() + 1);
                    resWorld.setSpawnLocation(spawnLocation);

                    Block blockAt1 = world_nether.getBlockAt(spawnLocation1);
                    blockAt1.setType(Material.BEDROCK);
                    spawnLocation1.setY(spawnLocation1.getY() + 1);
                    world_nether.setSpawnLocation(spawnLocation1);

                    Block blockAt2 = world_the_end.getBlockAt(spawnLocation2);
                    blockAt2.setType(Material.BEDROCK);
                    spawnLocation2.setY(spawnLocation2.getY() + 1);
                    world_the_end.setSpawnLocation(spawnLocation2);
                    System.out.println("§a成功为世界设置出生点");
                    // 重置世界游戏规则
                    TCUtil.setServerWorldsGameRule();
                }, 20 * 30);
            }
        }, 0, 20 * 60);
    }

}
